"""
Models for the Live application.
"""

from django.db import models

# We need the authentication application's models for user handling
from django.contrib.auth.models import User

# We need the "Track" application's models for tagging
from track.models import Vehicle

class Tag(models.Model):
  """Stores information regarding tags"""
  name      = models.CharField(max_length=200, unique=True)
  slug      = models.SlugField(
                prepopulate_from=('name',), unique=True,
                help_text='This field is used for urls, etc.')
  user      = models.ForeignKey(User)
  is_public = models.BooleanField(
                help_text='If True, then this tag will be visible to all users')
  
  def __unicode__(self):
    return '%(name)s (%(user)s, %(public)s)'%{
      'name':   self.name,
      'user':   self.user,
      'public': 'public' if self.is_public else 'private'}
  
  class Meta:
    unique_together = ('user', 'name')
  
  class Admin:
    list_filter     = ('user', 'is_public')
    search_fields   = ('name', 'slug', 'user__username')
  

class VehicleTag(models.Model):
  """Stores associations between Vehicles and Tags"""
  vehicle = models.ForeignKey(Vehicle)
  tag     = models.ForeignKey(Tag)
  
  def __unicode__(self):
    return '%(vehicle)s <=> %(tag)s'%{
      'vehicle': self.vehicle.name,
      'tag':     self.tag.name}
  
  class Meta:
    unique_together = ('vehicle', 'tag')
    verbose_name    = 'Vehicle<->Tag association'
  
  class Admin:
    list_filter     = ('vehicle', 'tag')
    search_fields   = ('vehicle__name', 'tag__name', 'vehicle__slug', 'tag__slug')

class UserProfile(models.Model):
  """Additional information about the User such as options, etc."""
  user    = models.OneToOneField(User)
  options = models.TextField(
              blank=True, core=True, editable=False,
              help_text='A blob-like object that stores serialized data provided by the js UI')
  
  def __unicode__(self):
    return unicode(self.user)
  
  class Admin:
    search_fields   = ('user__username',)
  
